# -*- coding: utf-8 -*-

from django import newforms as forms
from django.newforms import widgets
from django.contrib.auth.models import User

class LoginForm(forms.Form):
    username = forms.CharField(max_length=100, label="Uporabniško ime")
    password = forms.CharField(max_length=100, widget=widgets.PasswordInput(), label="Geslo")
    remember = forms.BooleanField(required=False, label="Zapomni si me")
    do_login = forms.CharField(initial="True", widget=widgets.HiddenInput(), required=False)
    
class RegistrationForm(forms.Form):
    username = forms.CharField(max_length=100, label="Uporabniško ime")
    password1 = forms.CharField(max_length=100, widget=widgets.PasswordInput(), label="Geslo")
    password2 = forms.CharField(max_length=100, widget=widgets.PasswordInput(), label="Geslo ponovno")
    email = forms.EmailField(max_length=100, label="E-pošta")
    registration = forms.CharField(initial="True", widget=widgets.HiddenInput(), required=False)
    
    def clean_username(self):
        get = self.clean_data.get
        try:
            # try to get user from databse
            User.objects.get(username__exact=get("username"))
        except User.DoesNotExist:
            # user doesn't exists, everything is ok
            return self.clean_data["username"]
        # user exists, return error
        raise forms.ValidationError(u"Uporabnik &#382;e obstaja")
        
    def clean_password2(self):
        get = self.clean_data.get
        # check if password1 == password2
        if not get("password1") == get("password2"):
            raise forms.ValidationError(u"Gesli se ne ujemata")
        return self.clean_data["password2"]
        
    def save(self):
        # create user from valid data
        get = self.clean_data.get
        u = User.objects.create_user(get("username"), get("email"), get("password1"))
        # set user inactive until he visits email verification link
        u.is_active = False
        u.save()
        return u
        
class ProfileForm(forms.Form):
    first_name = forms.CharField(max_length=100, label="Ime", required=False)
    last_name = forms.CharField(max_length=100, label="Priimek", required=False)
    company = forms.CharField(max_length=100, label="Podjetje", required=False)
    status = forms.CharField(max_length=100, label="Status", required=False, widget=widgets.Select(choices=(('dijak', 'dijak'), ('student', 'študent'), ('zaposlen', 'redno zaposlen'), ('upokojenec', 'upokojenec'), ('ostalo', 'ostalo'))))
    signature = forms.CharField(max_length=255, label="Podpis na forumu", required=False, widget=widgets.Textarea())
    public_email = forms.BooleanField(label="Javni e-poštni naslov", required=False)
    send_notices = forms.BooleanField(label="Obveščaj me o dogodkih/novostih", required=False)